<template>
    <van-sticky>
        <van-search class="search-body" v-model="keyword" shape="round" show-action background="#fb7299" @search="onSearch"
            placeholder="请输入搜索关键词">
            <template #action>
                <div class="search-btn" @click="onSearch">搜索</div>
            </template>
        </van-search>
        <van-tabs v-model:active="channelId" @click-tab="changeChannel" shrink animated swipeable :lazy-render="false">
            <van-tab v-for="item in channelList" :key="item.type_id" :name="item.type_id" :title="item.type_name"></van-tab>
        </van-tabs>
    </van-sticky>
    <van-list v-model:loading="loading" :finished="finished" :immediate-check="check" finished-text="精彩内容马上来~"
        @load="onLoad">
        <div class="video-list">
            <AppVideo v-for="item in videoList" :key="item.vod_id" :item="item" />
        </div>
    </van-list>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import type { ChannelVO, Class } from '@/types/ChannelVO'
import type { VideoVO, Video } from '@/types/VideoVO'

const page = ref(1)
const keyword = ref('')
const loading = ref(false)
const finished = ref(false)
const check = ref(false)
const channelList = ref<Class[]>([])
const videoList = ref<Video[]>([])
const channelId = useChannelId()
const config = useConfig()

function loadChannelLit() {
    const url = config.value?.url
    useFetch('/api/video', { query: { ac: 'list' } }).then(res => {
        const data = JSON.parse(res.data.value) as ChannelVO
        channelList.value = data.class.filter(v => !config.value?.exclude_channel_ids.includes(v.type_id))
        channelList.value.unshift({type_id: 0, type_pid: 0, type_name: '首页'})
    })
}

function onSearch() {
    changeChannel()
}

function changeChannel() {
    page.value = 1
    videoList.value = []
    loading.value = false
    finished.value = true
    onLoad()
}

function onLoad() {
    // 根据当前页码提取数据,并追加到用于渲染的数组中
    useFetch('/api/video', { query: { ac: 'detail', t: channelId.value, pg: page.value, wd: keyword.value } }).then(res => {
        const data = JSON.parse(res.data.value) as VideoVO
        videoList.value.push(...data.list)
        // 数据全部加载完成,不能再继续下滑加载数据了
        if (data.total <= videoList.value.length) {
            finished.value = true
        } else {
            finished.value = false
            page.value++
        }
        loading.value = false
    })
}

loadChannelLit()
onLoad()
</script>

<style lang="scss" scoped>
// 视频列表
.video-list {
    display: flex;
    flex-wrap: wrap;
    padding: 10px 5px;
}

// 视频卡片
.v-card {
    width: 50%;
    padding: 0 5px 10px;

    .card {
        position: relative;
        background: #f3f3f3 url(@/assets/images/default.png) center no-repeat;
        background-size: 36%;
        border-radius: 2px;
        overflow: hidden;

        .card-img {
            .pic {
                height: 100px;
                width: 100%;
                object-fit: cover;
            }
        }

        .count {
            background-image: linear-gradient(0deg, #000000d9, #0000);
            color: #fff;
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            font-size: 12px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 5px 6px;

            span {
                .iconfont {
                    font-size: 12px;
                }
            }
        }
    }

    .title {
        margin-top: 5px;
        font-size: 12px;
        color: #212121;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }
}
</style>